package com.controller;

import com.alibaba.druid.util.StringUtils;
import com.dto.UploadFileDto;
import com.entity.SysFile;
import com.service.impl.FileUploadServiceImpl;
import com.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;



@Api(tags = "文件上传API")
@RestController
@RequestMapping("/file")
public class UploadController {

    @Autowired
    FileUploadServiceImpl fileUploadService;



    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public R<SysFile> uploadFile(
                    @ApiParam(name = "dto",value = "文件dto",required = true)
                    UploadFileDto dto
    ) throws IOException {


            SysFile sysFile = null;
            Boolean chunkFlag = dto.getChunkFlag();
            if (chunkFlag) {
                sysFile = fileUploadService.chunkFileUpload(dto);
            }else {
                sysFile = fileUploadService.singleFileUpload(dto);
            }
            return R.success(sysFile);

    }

    @ApiOperation(value = "文件检查是否存在")
    @PostMapping("/checkMd5")
    public R<SysFile> checkMd5(
            @ApiParam(name = "md5",value = "文件md5",required = true)
            String md5,
            @ApiParam(name = "userId",value = "用户id",required = true)
            Integer userId
    ){
        SysFile sysFile = fileUploadService.getSysFile(md5,userId);
        if (sysFile == null){
            return R.success(null);
        }else {
            return R.success(sysFile);
        }
    }
}
